package org.dromara.book.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.book.util.ISBNUtil;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.book.domain.vo.SzlyBaseIsbnVo;
import org.dromara.book.domain.bo.SzlyBaseIsbnBo;
import org.dromara.book.service.ISzlyBaseIsbnService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * ISBN库
 * 前端访问路由地址为:/book/baseIsbn
 *
 * @author LionLi
 * @date 2024-03-19
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/baseIsbn")
public class SzlyBaseIsbnController extends BaseController {

    private final ISzlyBaseIsbnService szlyBaseIsbnService;

    /**
     * 查询ISBN库列表
     */
    @SaCheckPermission("book:baseIsbn:list")
    @GetMapping("/list")
    public TableDataInfo<SzlyBaseIsbnVo> list(SzlyBaseIsbnBo bo, PageQuery pageQuery) {
        return szlyBaseIsbnService.queryPageList(bo, pageQuery);
    }



    /**
     * 根据isbn查询数据
     *
     * @param isbn 主键
     */
    @SaCheckPermission(value = {"book:baseIsbn:add","book:baseIsbn:edit"})
    @GetMapping("/queryByIsbn")
    public R<Map<String, List<SzlyBaseIsbnVo>>> queryByIsbn(String  isbn) {
        szlyBaseIsbnService.queryByIsbn(isbn);
        Map<String, List<SzlyBaseIsbnVo>> result = new HashMap<>();
        List<SzlyBaseIsbnVo> apiList = new ArrayList<>();
        apiList.addAll(ISBNUtil.queryAliyun(isbn));
        result.put("local",szlyBaseIsbnService.queryByIsbn(isbn));
        result.put("apiList",apiList);
        return R.ok(result);
    }

    /**
     * 导出ISBN库列表
     */
    @SaCheckPermission("book:baseIsbn:export")
    @Log(title = "ISBN库", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SzlyBaseIsbnBo bo, HttpServletResponse response) {
        List<SzlyBaseIsbnVo> list = szlyBaseIsbnService.queryList(bo);
        ExcelUtil.exportExcel(list, "ISBN库", SzlyBaseIsbnVo.class, response);
    }

    /**
     *
     * 获取ISBN库详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission(value = {"book:baseIsbn:query"})
    @GetMapping("/{id}")
    public R<SzlyBaseIsbnVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(szlyBaseIsbnService.queryById(id));
    }

    /**
     * 新增ISBN库
     */
    @SaCheckPermission("book:baseIsbn:add")
    @Log(title = "ISBN库", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<SzlyBaseIsbnBo> add(@Validated(AddGroup.class) @RequestBody SzlyBaseIsbnBo bo) {
        String isbn13 = bo.getIsbn13();
        List<SzlyBaseIsbnVo> szlyBaseIsbnVos = szlyBaseIsbnService.queryByIsbn(isbn13);
        if(!szlyBaseIsbnVos.isEmpty()){
            return R.warn("ISBN重复,不能新增");
        }
        return R.ok(szlyBaseIsbnService.insertByBo(bo));
    }

    /**
     * 修改ISBN库
     */
    @SaCheckPermission("book:baseIsbn:edit")
    @Log(title = "ISBN库", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<SzlyBaseIsbnBo> edit(@Validated(EditGroup.class) @RequestBody SzlyBaseIsbnBo bo) {
        return R.ok(szlyBaseIsbnService.updateByBo(bo));
    }

    /**
     * 删除ISBN库
     *
     * @param ids 主键串
     */
    @SaCheckPermission("book:baseIsbn:remove")
    @Log(title = "ISBN库", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(szlyBaseIsbnService.deleteWithValidByIds(List.of(ids), true));
    }
}
